package com.buli.blmall.admin.system.service;

import com.buli.blmall.admin.system.entity.SysUserRole;
import com.buli.blmall.admin.system.vo.SysUserRoleVO;
import com.buli.blmall.admin.system.mapper.SysUserRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;

/**
 * <p>
 * 用户角色 服务类
 * </p>
 *
 * @author buli
 * @since 2025-02-27
 */
@Service
public class SysUserRoleService extends ServiceImpl<SysUserRoleMapper, SysUserRole> {

    /**
     * 查询用户角色详情
     *
     * @param id
     * @return 用户角色详情
     */
    public SysUserRoleVO info(Long id) {
        return BeanUtil.copyProperties(getById(id), SysUserRoleVO.class );
    }

    /**
     * 查询用户角色集合
     *
     * @param sysUserRoleVo
     * @return 用户角色集合
     */
    public List<SysUserRoleVO> queryList(SysUserRoleVO sysUserRoleVo) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        List<SysUserRole> list = list(queryWrapper);
        return BeanUtil.copyToList(list, SysUserRoleVO.class );
    }

    /**
     * 新增用户角色
     * @param sysUserRoleVo
     * @return boolean
     */
    public boolean add(SysUserRoleVO sysUserRoleVo) {
        SysUserRole sysUserRole = BeanUtil.copyProperties(sysUserRoleVo, SysUserRole.class );
        return save(sysUserRole);
    }

    /**
     * 编辑用户角色
     * @param sysUserRoleVo
     * @return boolean
     */
    public boolean edit(SysUserRoleVO sysUserRoleVo) {
        SysUserRole sysUserRole = BeanUtil.copyProperties(sysUserRoleVo, SysUserRole.class );
        return updateById(sysUserRole);
    }

    /**
     * 删除用户角色
     * @param id
     * @return boolean
     */
    public boolean delete(Long id) {
        return removeById(id);
    }

    /**
     * 获取用户角色集合
     * @param userId
     * @return
     */
    public List<SysUserRole> getListByUserId(Long userId) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        return list(queryWrapper);
    }

}
